package com.iknight.theory.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iknight.theory.api.TeTgService;
import com.iknight.theory.mapper.*;
import com.iknight.theory.model.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeTgServiceImpl implements TeTgService<TETgModel, String> {

    private final static Logger logger = LogManager.getLogger(TeTgServiceImpl.class);
    @Autowired
    private TeTgMapper<TETgModel, String> teTgMapper;
    @Autowired
    private TeTranMapper<TETgModel, String> teTranMapper;
    @Autowired
    private TeLineMapper<TELineModel, String> teLineMapper;
    @Autowired
    private TeRecordMapper<TPRecordModel, String> teRecordMapper;
    @Autowired
    private TeConsMapper<TEConsModel, String> teConsMapper;
    @Autowired
    private TeLineSegMapper<TELineSegModel, String> teLineSegMapper;
    @Autowired
    private TeLinePoleMapper<TELinePoleModel, String> teLinePoleMapper;
    @Autowired
    private TeContainerMapper<TEConsModel, String> teContainerMapper;
    @Autowired
    private TeLineConsMapper<TELineConsModel, String> teLineConsMapper;
    @Autowired
    private TPCurrentMapper<TPCurrentModel, String> tpCurrentMapper;

    @Override
    public void save(TETgModel record) {
        if (null == teTgMapper.selectOne(record.getTg_no())) {
            teTgMapper.insert(record);
        } else {
            teTgMapper.update(record);
        }
    }

    /**
     * 增加事务注解，删除台区时，将级联删除以下关联设备数据
     * 1.配电变压器
     * 2.低压线路
     * 3.低压线段
     * 4.低压杆塔
     * 5.计量箱
     * 6.低压用户
     * 7.电量数据
     *
     * @param pkey
     */
    @Override
    @Transactional
    public void delete(String pkey) {
        //获取变压器数据
        Map tranParam = new HashMap();
        tranParam.put("tg_id", pkey);
        List<TETranModel> tranls = teTranMapper.selectByCond(tranParam);
        //获取线路数据
        Map lineParam = new HashMap();
        lineParam.put("tg_id", pkey);
        List<TELineModel> linels = teLineMapper.selectByCond(lineParam);
        //获取用户数据
        Map consParam = new HashMap();
        consParam.put("tg_id", pkey);
        List<TEConsModel> consls = teConsMapper.selectByCond(consParam);
//        step 1 删除电量数据
        for (TETranModel tran : tranls) {//删除配变关联的数据
            teRecordMapper.removeDataByEquipId(tran.getTran_id());
            teRecordMapper.removeTranRecordByTranId(tran.getTran_id());
            //删除电流数据
            tpCurrentMapper.removeRecordByTranId(tran.getTran_id());
        }
        for (TEConsModel cons : consls) {
            teRecordMapper.removeDataByEquipId(cons.getCons_id());
        }
//        step 2 删除用户
        teConsMapper.removeDataByTgId(pkey);
        for (TELineModel line : linels) {
            String lineid = line.getLine_id();

            //删除分段数据
            Map _tempParam = new HashMap();
            _tempParam.put("line_id", lineid);
            List<TELineSegModel> segs = teLineSegMapper.selectByCond(_tempParam);

            for (TELineSegModel seg : segs) {
                teRecordMapper.removeSegRecordBySegId(seg.getSeg_id());
            }

            //step 3 删除进户线
            teLineConsMapper.removeDataByLineId(lineid);
            //step 4 删除表箱
            teContainerMapper.removeDataByLine(lineid);
            //step 5 删除杆塔
            teLinePoleMapper.removeDataByLineId(lineid);
            //step 6 删除分段
            teLineSegMapper.removeDataByLineId(lineid);
        }
//        step 7 删除线路
        teLineMapper.removeDataByTgId(pkey);
//        step 8 删除配变
        teTranMapper.removeDataByTgId(pkey);
//        step 9 删除台区
        teTgMapper.delete(pkey);
    }

    @Override
    public TETgModel selectOne(String pkey) {
        TETgModel result = teTgMapper.selectOne(pkey);
        return result;
    }

    @Override
    public PageInfo<TETgModel> selectPage(Map<String, Object> params, int pageStart, int pageSize) {
        PageHelper.startPage(pageStart, pageSize, true);
        List<TETgModel> result = teTgMapper.selectByCond(params);
        PageInfo<TETgModel> page = new PageInfo<>(result);

        return page;
    }

    @Override
    public List<Map> getTgDirectionary() {
        return teTgMapper.getTgDirectionary();
    }
}
